package com.njit.WeStudy.dao;

import com.njit.WeStudy.dataobject.JoinClass;
import java.util.List;

import com.njit.WeStudy.dataobject.Student;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

public interface JoinClassMapper {
    @Delete({
        "delete from join_class",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    @Insert({
        "insert into join_class (user_id, ",
        "class_id, join_time, ",
        "leave_time, is_top)",
        "values ( #{userId,jdbcType=INTEGER}, ",
        "#{classId,jdbcType=INTEGER}, now(), ",
        "#{leaveTime,jdbcType=TIMESTAMP}, #{isTop,jdbcType=BIT})"
    })
    int insert(JoinClass record);

    @Select({
        "select",
        "id, user_id, class_id, join_time, leave_time, is_top",
        "from join_class",
        "where id = #{id,jdbcType=INTEGER}"
    })
    @Results(id = "ResultMap",value={
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
        @Result(column="class_id", property="classId", jdbcType=JdbcType.INTEGER),
        @Result(column="join_time", property="joinTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="leave_time", property="leaveTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="is_top", property="isTop", jdbcType=JdbcType.BIT)
    })
    JoinClass selectByPrimaryKey(Integer id);

    @Select({
        "select",
        "id, user_id, class_id, join_time, leave_time, is_top",
        "from join_class"
    })

    @ResultMap("ResultMap")
    List<JoinClass> selectAll();

    @Select({
            "select",
            "id, join_class.user_id, class_id, join_time, leave_time, is_top,user_name,head_image",
            "from join_class,user",
            "where join_class.user_id=user.user_id"
    })
    @Results(id = "ResultStudentMap",value={
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
            @Result(column="class_id", property="classId", jdbcType=JdbcType.INTEGER),
            @Result(column="join_time", property="joinTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="leave_time", property="leaveTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="is_top", property="isTop", jdbcType=JdbcType.BIT),
            @Result(column="user_name", property="userName", jdbcType=JdbcType.VARCHAR),
            @Result(column="head_image", property="headImage", jdbcType=JdbcType.VARCHAR)
    })
    List<Student> selectAllStudents();

    @SelectProvider(type = QueryBuilder.class,method = "joinClassQueryByClassId")
    @ResultMap("ResultStudentMap")
    List<Student> joinClassQueryByClassId(@Param("classId") int classId);

    //查询到当前用户参加的所有班级的id(加上.getClassId())
    @SelectProvider(type = QueryBuilder.class,method = "joinClassQueryByUserId")
    @ResultMap("ResultMap")
    List<JoinClass> joinClassQueryByUserId(@Param("userId")int userId);

    @Update({
        "update join_class",
        "set user_id = #{userId,jdbcType=INTEGER},",
          "class_id = #{classId,jdbcType=INTEGER},",
          "leave_time = #{leaveTime,jdbcType=TIMESTAMP},",
          "is_top = #{isTop,jdbcType=BIT}",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(JoinClass record);
}